package com.kly.cashmall.utils.system;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.kly.cashmall.R;
import com.kly.cashmall.utils.function.ToastUtil;
import com.kly.cashmall.utils.data.CollectionUtils;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import java.util.List;


/**
 * @author
 * @date 2018/12/5
 */
public class PermissionUtils {

    /**
     * 判断是否缺少权限
     */
    public static boolean lacksPermission(Context mContexts, String permission) {
        return ContextCompat.checkSelfPermission(mContexts, permission) == PackageManager.PERMISSION_DENIED;
    }

    @SuppressLint("WrongConstant")
    public static void requestPermission(Context context, Action<List<String>> granted,
                                         String... permissions) {
        AndPermission.with(context)
                .runtime()
                .permission(permissions)
                .onGranted(granted)
                .onDenied(denied -> {
                    if (AndPermission.hasAlwaysDeniedPermission(context, denied)) {
                        List<String> permissionNames = Permission.transformText(context, denied);
                        if (!CollectionUtils.isEmpty(permissionNames)) {

//              if (context instanceof Activity && !((Activity) context).isFinishing()) {
                            ToastUtil.showToast(context.getResources().getString(R.string.system_requires)+":\n" + TextUtils.join("\n", permissionNames),
                                    Toast.LENGTH_LONG);
                            return;
//              }
                        }
                    }
                    ToastUtil.showToast(context.getResources().getString(R.string.access_failure));
                }).start();
    }

    public static void goSetting(Context context) {
        AndPermission.with(context).runtime().setting().start(1);
    }

    public static void requestPermission(Context context, Action<List<String>> granted,
                                         Action<List<String>> denied, String... permissions) {
        AndPermission.with(context)
                .runtime()
                .permission(permissions)
                .onGranted(granted)
                .onDenied(denied)
                .start();
    }

    /**
     * 获取应用必须的权限：读写权限、手机信息权限
     */
    public static void requestMustPermission(Context context, Action<List<String>> granted,
                                             Action<List<String>> denied) {
        if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.P){
            PermissionUtils.requestPermission(context, data -> {
                PermissionUtils.requestPermission(context, granted, denied, Permission.READ_PHONE_STATE);
            }, denied, Permission.Group.STORAGE);
        } else {
            PermissionUtils.requestPermission(context, granted, denied, Permission.Group.STORAGE);
        }
    }

    public static boolean hasMustPermission(Context context) {
        return AndPermission.hasPermissions(context, Permission.Group.STORAGE)
                && AndPermission.hasPermissions(context, Permission.READ_PHONE_STATE);
    }
}
